/**
 * Created by BigBi on 2017/6/4.
 */
/**
 * Created by BigBi on 2017/6/4.
 */
var express = require('express');
var fs = require("fs");
var bodyParser = require('body-parser');
var multer  = require('multer');

var app = new express();
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(multer({ dest: '/tmp/'}).array('image'));

app.get('/upload.html',function(req,res){
    res.sendFile(__dirname + "/" + "upload.html");
});

app.post('/file_upload', function (req, res) {

    console.log(req.files[0]);  // 上传的文件信息

    var des_file = __dirname + "/public/upload/" + req.files[0].originalname;//文件保存路径+文件名
    fs.readFile( req.files[0].path, function (err, data) {
        fs.writeFile(des_file, data, function (err) {
            if( err ){
                console.log( err );
            }else{
                response = {
                    message:'File uploaded successfully',
                    filename:req.files[0].originalname,
                    savepath:des_file
                };
            }
            console.log( response );
            res.end( JSON.stringify( response ) );
        });
    });
})

var server = app.listen(8000,function(){
    console.log('文件上传服务已开启');
});